<template>
	<view class="container">
		<u-form :model="form" ref="uForm" label-position="top" :error-type="['toast']">
			<u-form-item label="宝宝">
				<view class="children-view">
					<children-list-view :addShow="addShow()" @showClasses="showClasses" :childrenList="childrenList"
						@itemClick="childrenItemClick"></children-list-view>
				</view>
			</u-form-item>
			<u-form-item label="入睡时间" prop="sleepTime">
				<u-input v-model="form.sleepTime" type="select" placeholder="请选择时间" @click="timeShow1 = true" />
			</u-form-item>
			<u-form-item label="苏醒时间" prop="wakeupTime">
				<u-input v-model="form.wakeupTime" type="select" placeholder="请选择时间" @click="timeShow2 = true" />
			</u-form-item>
			<u-form-item label="睡眠质量" prop="rankVal">
				<u-rate v-model="form.rankVal" size="40" active-color="#f3bb2e"></u-rate>
			</u-form-item>
<!-- 			<u-form-item label="状况">
				<u-radio-group v-model="radio" width="25%">
					<u-radio v-for="(item, index) in radioList" :key="index" :name="item.value"
						:disabled="item.disabled">
						{{ item.name }}
					</u-radio>
				</u-radio-group>
			</u-form-item> -->
			<u-form-item label="备注" prop="remark">
<!-- 				<u-input v-model="form.remark" /> -->
					<textarea v-model="form.remark" rows="3" placeholder="请输入备注"/>
			</u-form-item>
		</u-form>
		<u-button class="action-view" circle @click="saveClick">保存</u-button>
		<u-picker v-model="timeShow1" mode="time" :params="params" @confirm="confirmTime1"></u-picker>
		<u-picker v-model="timeShow2" mode="time" :params="params" @confirm="confirmTime2"></u-picker>
		<children-select-view ref="popupConpare" @confirm="confirm"></children-select-view>
	</view>
</template>

<script>
	import {
		addRecordSleep,
		getRecordSleep,
	} from '@/api/record.js'
	import ChildrenListView from '@/components/children-list-view.vue'
	import ChildrenSelectView from '@/components/children-select-view.vue'
	import {
		formatDateByTimestamp
	} from '@/utils/utils.js'
	import recordMixin from './mixins/index.js'
	export default {
		mixins: [recordMixin],
		components: {
			ChildrenListView,
			ChildrenSelectView
		},
		data() {
			return {
				form: {
					eventTime: formatDateByTimestamp(Date.now(), 'yyyy/mm/dd hh:MM:ss'),
					sleepTime: formatDateByTimestamp(Date.now(), 'yyyy/mm/dd hh:MM:ss'),
					wakeupTime: '',
					rankVal: '',
					remark: '',
				},
				rules: {
					sleepTime: [{
						required: true,
						message: '请选择入睡时间',
					}, ],
					wakeupTime: [{
						required: true,
						message: '请选择苏醒时间',
					}, ],
					rankVal: [{
						required: true,
						message: '请选择睡眠质量',
					}, ],
				},
				
				radioList: [{
						name: '安稳',
					},
					{
						name: '普通',
					},
					{
						name: '不安稳'
					},
					{
						name: '易惊醒'
					}
				],
				radio: '',

				timeShow1: false,
				timeShow2: false,
				params: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
					second: false,
					timestamp: true
				},
			}
		},
		computed:{
			typeInfos() {
				return this.infoType
			}
		},
		methods: {
			addRecord() {
				let childIds = '';
				for (let index in this.childrenList) {
					let item = this.childrenList[index];
					childIds += `,${item.id}`;
				}
				if (childIds.length > 0) {
					childIds = childIds.substring(1);
				}
				this.form.eventTime = this.form.sleepTime;
				if (!this.$u.test.isEmpty(this.recordId)) {
					this.form.id = this.recordId
					this.form.childId = childIds
				}else {
					this.form.childIds = childIds
				}
				addRecordSleep(this.form).then(res => {
					this.$u.toast('添加成功');
					setTimeout(function(){
						uni.navigateBack({delta:1})
					},1000)
				})
			},

			confirmTime1(res) {
				console.log(res);
				this.form.sleepTime = formatDateByTimestamp(res.timestamp, 'yyyy/mm/dd hh:MM:ss');
			},
			confirmTime2(res) {
				console.log(res);
				this.form.wakeupTime = formatDateByTimestamp(res.timestamp, 'yyyy/mm/dd hh:MM:ss');
			},
			
			getRecord() {
				console.log(this.recordId)
				if (this.$u.test.isEmpty(this.recordId)) {
					return;
				}
				uni.showLoading({})
				const params = {
					id: this.recordId,
					childId: this.defaultChildren.id
				}
					getRecordSleep(params).then(res => {
						uni.hideLoading()
						const { rankVal, remark } = res.data; 
						const eventTime = formatDateByTimestamp(parseFloat(res.data.eventTime), 'yyyy/mm/dd hh:MM:ss')
						const sleepTime = formatDateByTimestamp(parseFloat(res.data.sleepTime), 'yyyy/mm/dd hh:MM:ss')
						const wakeupTime = formatDateByTimestamp(parseFloat(res.data.wakeupTime), 'yyyy/mm/dd hh:MM:ss')
						const form = {
							eventTime,
							sleepTime,
							wakeupTime,
							rankVal,
							remark,
						};
						this.form = form
					})

			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $app-page-bg-color;
	}
</style>
<style lang="scss" scoped>
	@import '@/css/u-form.scss';
	/deep/ .u-btn {
		border: 0 !important;
		color: white !important; 
		border-radius: 50rpx !important;
		background-image: linear-gradient(to right,#f3ba30, #d1c32a , #b0cb23) !important;
	}
	.container {
		padding-left: 30rpx;
		padding-right: 30rpx;
		display: flex;
		flex-direction: column;
	}

	.children-view {
		width: 10rpx;
		flex: 1;
		display: flex;
		flex-direction: row;
	}
	.action-view {
		margin-top: 0 !important;
	}
</style>
